'''
Created on Apr 26, 2012

@author: lavang
'''
import unittest
from Interface.announcementPage import xpathBtnGoToAnnouncement,\
    xpathLinkAddAnnouncement, xpathTxtAnnouncementTitle, nameChkCalendarEvent,\
    nameChkSendNotification, nameCbbStartDay, nameCbbStartMonth,\
    nameCbbStartYear, nameCbbStartHour, nameCbbStartMin, nameCbbExpireDay,\
    nameCbbExpireMonth, nameCbbExpireYear, nameCbbExpireHour, nameCbbExpireMin,\
    xpathBtnSubmit, xpathIconEdit, xpathTxtFilter
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys

# go to announcement page
def clickOnGoToAnnouncement(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathBtnGoToAnnouncement())
    element.click()
    
# click add announcement
def clickOnLinkAddAnnouncement(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathLinkAddAnnouncement())
    element.click()
 
# create an announcement
def createAnnouncement(self,title,day,month,year,hour,mins,eDay,eMonth,eYear,eHour,eMins,content,createEvent='on',sendNotification='on'):
    browser=self.browser
    frame=browser.switch_to_frame("POPUP_FRAME")
    element=browser.find_element_by_xpath(xpathTxtAnnouncementTitle())
    element.clear()
    element.send_keys(title)
   
    # create event
    element=browser.find_element_by_name(nameChkCalendarEvent())
    if createEvent=='on':
        if element.is_selected():
            pass
        else:
            element.click()
    elif createEvent=='off':
        if element.is_selected():
            element.click()
        else:
            pass
    else:
        pass
    
    # send notification
    element=browser.find_element_by_name(nameChkSendNotification())
    if sendNotification=='on':
        if element.is_selected():
            pass
        else:
            element.click()
    elif sendNotification=='off':
        if element.is_selected():
            element.click()
        else:
            pass
    else:
        pass
    
    #Select start day, month, year, hour, minute
    element=browser.find_element_by_name(nameCbbStartDay())
    Select(element).select_by_visible_text(day)
    
    element=browser.find_element_by_name(nameCbbStartMonth())
    Select(element).select_by_visible_text(month)
    
    element=browser.find_element_by_name(nameCbbStartYear())
    Select(element).select_by_visible_text(year)
    
    element=browser.find_element_by_name(nameCbbStartHour())
    Select(element).select_by_visible_text(hour)
    
    element=browser.find_element_by_name(nameCbbStartMin())
    Select(element).select_by_visible_text(mins)
    
    #Select expire day,month,year,hour,min
    element=browser.find_element_by_name(nameCbbExpireDay())
    Select(element).select_by_visible_text(eDay)
    
    element=browser.find_element_by_name(nameCbbExpireMonth())
    Select(element).select_by_visible_text(eMonth)
    
    element=browser.find_element_by_name(nameCbbExpireYear())
    Select(element).select_by_visible_text(eYear)
    
    element=browser.find_element_by_name(nameCbbExpireHour())
    Select(element).select_by_visible_text(eHour)
    
    element=browser.find_element_by_name(nameCbbExpireMin())
    Select(element).select_by_visible_text(eMins)
 
    # input content
    frame=browser.switch_to_frame("data_ifr")
    element=browser.find_element_by_xpath(".//*[@id='tinymce']")
    element.send_keys(Keys.CONTROL, 'a')
    element.send_keys(Keys.BACK_SPACE)
    element.send_keys(content)
    
    # complete creating announcement
    frame=browser.switch_to_default_content()
    frame=browser.switch_to_frame("POPUP_FRAME")
    element=browser.find_element_by_xpath(xpathBtnSubmit())
    element.click()
    
# verify that announcement is created successfully
def isAnnouncementCreatedSuccessfully(self,announcementName):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath(".//td[text()='"+announcementName+"']"))
        return True
    except:
        return False
    
# delete annoucement
def deleteAnnouncement(self,announcementName):
    browser=self.browser
    element=browser.find_element_by_xpath(".//td[text()='"+announcementName+"']/../td[5]/img")
    element.click()
    alert=browser.switch_to_alert()
    alert.accept()
    browser.refresh()
    
# verify delete successfully
def isAnnouncementDelete(self,announcementName):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath(".//td[text()='"+announcementName+"']"))
        return True
    except:
        return False

# click Edit
def clickOnIconEdit(self,announcementName):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathIconEdit(announcementName))
    element.click()

# seach announcement
def searchAnnouncement(self,announcementName):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathTxtFilter())
    element.clear()
    element.send_keys(announcementName)
    element.send_keys(Keys.RETURN)
    
def isAnnouncementExist(self,announcementName):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath(".//td[text()='"+announcementName+"']"))
        return True
    except:
        return False